Page({
  data:{
    input: '',
    todoItem: [],
    backlog: 3
  },

  save: function () {
    wx.setStorageSync('todo_list', this.data.todoItem)
  },

  load: function () {
    var todoItem = wx.getStorageSync('todo_list')
    if (todoItem) {
      var backlog = todoItem.filter(function (item) {
        return !item.completed
      }).length
      this.setData({ todoItem: todoItem, backlog: backlog })
    }
  },

  onLoad: function () {
    this.load()
  },

  getInputValueWW: function (e) {
    this.setData({ input: e.detail.value})
  },
  addItemWW: function () {
    var todoItem = this.data.todoItem
    if (!(this.data.input.trim()))return
    todoItem.push({
      name: this.data.input,
      templeted: false
    })
    this.data.input = ''
    this.setData({ todoItem: todoItem ,input: this.data.input })
    this.backlogWW()
    this.save()
  },
  toogleIiemWW: function (e) {
    var index = e.currentTarget.dataset.index
    var todoItem = this.data.todoItem
    todoItem[index].templeted = !todoItem[index].templeted
    this.setData({
      todoItem: todoItem 
    })
    this.backlogWW()
    this.save()
  },
  removeItemWW: function (e) {
    var index = e.currentTarget.dataset.index
    var todoItem = this.data.todoItem
    todoItem.splice(index,1)
    this.setData({
      todoItem: todoItem
    })
    this.backlogWW()
    this.save()
  },
  fulfillItemWW: function () {
    var t = this.data.todoItem
    if(this.data.backlog){
      t.forEach(item=>{
        if(!item.templeted){
          item.templeted = !item.templeted
        }
      })
    }else{
      t.forEach(item=>{
        if(item.templeted){
          item.templeted = !item.templeted
        }
      })
    }

    this.setData({ todoItem:t })
    this.backlogWW()
    this.save()
  },
  allRemoveItemWW: function () {
    var t = this.data.todoItem
    t.splice(0)
    this.setData({ todoItem: t })
    this.backlogWW()
    this.save()
  },
  backlogWW: function () {
    this.data.backlog = 0
    var t = this.data.todoItem
    t.forEach(item=>{
      if(!item.templeted) (this.data.backlog)++
    })
    this.setData({backlog:this.data.backlog})
  }
})